/**
* ...
* @author Default
* @version 0.1
*/

package bdz.common.utils {
	import flash.display.MovieClip;
	import fl.video.VideoEvent;
	import fl.video.FLVPlayback;
	import fl.video.VideoProgressEvent;
	import fl.video.MetadataEvent;
	import bdz.common.utils.*;
	import flash.events.*;
	import fl.transitions.easing.*;
	import flash.geom.Rectangle;
	
	public class VideoPlayerControl extends MovieClip {
		private var vid:FLVPlayback;
		private var pprogress:MovieClip;
		private var lprogress:MovieClip;
		private var player:MovieClip;
		private var stopper:MovieClip;
		private var bigplay:MovieClip;
		private var scrubber:MovieClip;
		private var base:MovieClip;
		private var duration:Number;
		function VideoPlayerControl():void{
			
		}
		private function initializeComponent():void{
			vid.addEventListener(VideoProgressEvent.PROGRESS, loadProgress);
			vid.addEventListener(VideoEvent.PLAYHEAD_UPDATE, checkPlayheadUpdate);
			vid.addEventListener(MetadataEvent.METADATA_RECEIVED, metaDataReceived);
			vid.addEventListener(VideoEvent.PLAYING_STATE_ENTERED, onPlay);
			vid.addEventListener(VideoEvent.PAUSED_STATE_ENTERED, onPause);
			vid.addEventListener(VideoEvent.COMPLETE, restartVideo);
			
			lprogress.scaleX = pprogress.scaleX = 0;
			scrubber.addEventListener(MouseEvent.MOUSE_DOWN, scrub);
			scrubber.addEventListener(MouseEvent.MOUSE_UP, scrub);
			player.useHandCursor = stopper.useHandCursor = stopper.buttonMode = player.buttonMode = true;
			bigplay.buttonMode = bigplay.useHandCursor = scrubber.buttonMode = scrubber.useHandCursor = true;
			stopper.addEventListener(MouseEvent.CLICK, pauseVideo);
			player.addEventListener(MouseEvent.CLICK, playVideo);
			bigplay.addEventListener(MouseEvent.CLICK, playVideo);
		}
		private function onRemovedFromStage(evt:Event):void {
			evt.target.stop();
		}
		private function restartVideo(evt:VideoEvent):void{
			vid.seek(0);
		}
		private function onPlay(evt:VideoEvent):void{
			player.enabled = false;
			stopper.enabled = true;
			TweenLite.to(bigplay, .8, {alpha:0, ease:Strong.easeOut});
			TweenFilterLite.to(player, .8, {type:"color", saturation:0, alpha:.6, ease:Strong.easeOut});
			TweenFilterLite.to(stopper, .8, {type:"color", saturation:1, alpha:1, ease:Strong.easeOut});
		}
		private function onPause(evt:VideoEvent):void{
			stopper.enabled = false;
			player.enabled = true;
			TweenLite.to(bigplay, .8, {alpha:1, ease:Strong.easeOut});
			TweenFilterLite.to(player, .8, {type:"color", saturation:1, alpha:1, ease:Strong.easeOut});
			TweenFilterLite.to(stopper, .8, {type:"color", saturation:0, alpha:.6, ease:Strong.easeOut});
		}
		private function checkPlayheadUpdate(evt:VideoEvent):void{
			pprogress.scaleX = evt.playheadTime/duration;
			scrubber.x = pprogress.x+pprogress.width;
		}
		private function loadProgress(evt:VideoProgressEvent):void{
			lprogress.scaleX = evt.bytesLoaded/evt.bytesTotal;
		}
		private function metaDataReceived(evt:MetadataEvent):void{
			duration = evt.info.duration;
		}
		private function checkScrubbing(prcnt:Number, _duration:Number) {
			return (prcnt*_duration);
		}
		private function checkScrub(evt:Event):void{
			pprogress.width = scrubber.x-pprogress.x;
			vid.ncMgr.videoPlayer.netStream.seek(checkScrubbing(pprogress.scaleX, duration));
		}
		private function scrub(evt:MouseEvent):void{
			if (evt.type == MouseEvent.MOUSE_DOWN) {
				
				scrubber.startDrag(false, new Rectangle(base.x, scrubber.y, base.width, 0));
				vid.parent.stage.addEventListener(MouseEvent.MOUSE_UP, scrub);
				vid.pause();
				addEventListener(Event.ENTER_FRAME, checkScrub);
			}else{
				scrubber.stopDrag();
				vid.play();
				vid.parent.stage.removeEventListener(MouseEvent.MOUSE_UP, scrub);
				removeEventListener(Event.ENTER_FRAME, checkScrub);
			}
		}
		private function pauseVideo(evt:MouseEvent):void{
			vid.pause();
		}
		private function playVideo(evt:MouseEvent):void{
			vid.play();
		}
		public function controlPlayer(flvp:FLVPlayback):void{
			vid = flvp;
			flvp.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
			initializeComponent();
		}
		public function set playOverlay(mc:MovieClip):void{
			bigplay = mc;
		}
		public function set stopButton(mc:MovieClip):void{
			stopper = mc;
		}
		public function set playButton(mc:MovieClip):void{
			player = mc;
		}
		public function set scrubButton(mc:MovieClip):void{
			scrubber = mc;
		}
		public function set baseProgressClip(mc:MovieClip):void{
			base = mc;
		}
		public function set playProgressClip(mc:MovieClip):void{
			pprogress = mc;
		}
		public function set loadProgressClip(mc:MovieClip):void{
			lprogress = mc;
		}
		public function get playOverlay():MovieClip{
			return bigplay;
		}
		public function get stopButton():MovieClip{
			return stopper;
		}
		public function get playButton():MovieClip{
			return player;
		}
		public function get scrubButton():MovieClip{
			return scrubber;
		}
		public function get playProgressClip():MovieClip{
			return pprogress;
		}
		public function get loadProgressClip():MovieClip{
			return lprogress;
		}
		public function get baseProgressClip():MovieClip{
			return base;
		}
	}
	
}